package com.xone.xapp;

import java.util.List;
import java.util.Locale;

import org.json.JSONObject;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class LinksGridAdapter extends BaseAdapter {
	
	protected Context context;
	protected List<JSONObject> list;
	protected int imageWidth;
	protected LayoutInflater mInflater;
	protected Resources resource;
	
	public LinksGridAdapter(Context context, List<JSONObject> list, int imageWidth) {
		this.context = context;
		this.list = list;
		this.imageWidth = imageWidth;
		resource = context.getResources();
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		if (null == list || list.isEmpty()) {
			return 0;
		}
		return list.size();
	}

	@Override
	public JSONObject getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = this.mInflater.inflate(R.layout.links_item, null);
			holder.setImageView((ImageView)view.findViewById(R.id.links_image));
			holder.setButtonPlus((Button)view.findViewById(R.id.button_plus));
			holder.setButtonRight((Button)view.findViewById(R.id.button_right));
			view.setTag(holder);
		} else {
			holder = (ViewHolder)view.getTag();
		}
		JSONObject object = list.get(position);
		String linkNo = null;
		Boolean selected = false;
		linkNo = JSONUtils.getStringFromJSONObject(object,"linkNo");
		selected = JSONUtils.getBooleanFromJSONObject(object, "selected");
		if (null != linkNo) {
			holder.setId(linkNo);
			holder.setSelected(selected);
			linkNo = linkNo.toLowerCase(Locale.ENGLISH);
		}
		final int resId = ContextUtils.getResourceId(context, linkNo, "drawable");
		holder.getImageView().setImageResource(resId);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewHolder viewHolder = (ViewHolder)v.getTag();
				new ImageClickTask(context).execute(viewHolder.getId());
				viewHolder.setSelected(!viewHolder.isSelected());
				setImageStatus(viewHolder);
				v.setTag(viewHolder);
//				Toast.makeText(context, "点击编号:" + viewHolder.getId(), Toast.LENGTH_SHORT).show();
			}
		});
		setImageStatus(holder);
		return view;
	}
	
	protected void setImageStatus(ViewHolder holder) {
		if (holder.isSelected()) {
			holder.getButtonRight().setVisibility(View.VISIBLE);
			holder.getButtonPlus().setVisibility(View.GONE);
		} else {
			holder.getButtonRight().setVisibility(View.GONE);
			holder.getButtonPlus().setVisibility(View.VISIBLE);
		}
	}
	
	/**
	 * 在图片上重叠画图片
	 * @param resId
	 * @param selected
	 * @return
	 */
	protected LayerDrawable getLinksImage(int resId, boolean selected) {
		Drawable [] layers = new Drawable[2];
		layers[0] = resource.getDrawable(resId);
		if (selected) {
			layers[1] = resource.getDrawable(R.drawable.plus_n);
		} else {
			layers[1] = resource.getDrawable(R.drawable.right_n);
		}
		LayerDrawable layerDrawable = new LayerDrawable(layers);
		layerDrawable.setLayerInset(1, 0, 0, 120, 120);
		return layerDrawable;
	}
	
	public class ViewHolder {
		protected String id;
		protected ImageView imageView;
		protected Button buttonPlus;
		protected Button buttonRight;
		protected boolean selected = false;
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public ImageView getImageView() {
			return imageView;
		}
		public void setImageView(ImageView imageView) {
			this.imageView = imageView;
		}
		public Button getButtonPlus() {
			return buttonPlus;
		}
		public void setButtonPlus(Button buttonPlus) {
			this.buttonPlus = buttonPlus;
		}
		public Button getButtonRight() {
			return buttonRight;
		}
		public void setButtonRight(Button buttonRight) {
			this.buttonRight = buttonRight;
		}
		public boolean isSelected() {
			return selected;
		}
		public void setSelected(boolean selected) {
			this.selected = selected;
		}
	}
	
	/**
	 * 图片点击执行的任务
	 * @author hunnyhu
	 */
	protected class ImageClickTask extends AsyncTask<String, Integer, String[]> {
		protected Context context;
		public ImageClickTask(Context context) {
			this.context = context;
		}
		@Override
		protected String[] doInBackground(final String... params) {
			if (isCancelled()) {
				return null;
			}
			if (null == params || params.length == 0) {
				return null;
			}
			return HttpUtils.httpGet(Constants.APP_URL + "/assistant/dolinkJson.html?linkno=" + params[0], null, 
					new HttpUtils.onGetSuccessListener<String[]>() {
				@Override
				public String[] onSuccess(int status, String result) {
					
					JSONObject jo = JSONUtils.getJSONObjectFromString(result);
					if (null != jo) {
						return new String [] {params[0], JSONUtils.getStringFromJSONObject(jo, "UR")};
					}
					return new String[] {params[0]};
				}
			}, context);
//			return params[0];
		}
		@Override
		protected void onPostExecute(String [] result) {
			super.onPostExecute(result);
			if (null == result) {
				return;
			}
//			if ("remove".equals(ur)) {
//				viewHolder[0].setSelected(false);
//			} else if ("create".equals(ur)) {
//				viewHolder[0].setSelected(true);
//			}
			Toast.makeText(context, "操作成功", Toast.LENGTH_LONG).show();
//			setImageStatus(result);
		}
	}

}
